package io.kiki.sba.registry.server.data_store.change;


import com.google.common.collect.Sets;
import io.kiki.sba.registry.common.model.TraceTimes;
import lombok.Getter;

import java.util.Collection;
import java.util.Set;

public class DataChangeMerger {
    @Getter
    private final Set<String> dataInfoIds = Sets.newConcurrentHashSet();
    private volatile DataChangeType lastDataChangeType;
    private long firstTs;

    public DataChangeMerger() {
    }

    public void addChanges(Collection<String> dataInfoIds, DataChangeType dataChangeType) {
        long now = System.currentTimeMillis();
        this.dataInfoIds.addAll(dataInfoIds);
        this.lastDataChangeType = dataChangeType;
        if (firstTs == 0) {
            firstTs = now;
        }
    }

    public void clear() {
        this.dataInfoIds.clear();
        this.firstTs = 0;
    }

    public TraceTimes createTraceTime() {
        TraceTimes traceTimes = new TraceTimes();
        traceTimes.setDataChangeType(lastDataChangeType.ordinal());
        traceTimes.setFirstDataChange(firstTs);
        return traceTimes;
    }

}
